package com.hgyc.mom.wms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.wms.entity.WmsStockRecord;
import com.hgyc.mom.wms.vo.WmsStockRecordParam;
import com.hgyc.mom.wms.service.WmsStockRecordService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 库存记录Controller
 *
 * @author fwj
 * @since 2025-06-26
 */
@Tag(name = "库存记录控制器", description = "库存记录控制器")
@RestController
@RequestMapping("/wms/stock/record")
public class WmsStockRecordController {
    @Autowired
    private WmsStockRecordService wmsStockRecordService;

    @Operation(summary = "查询库存记录列表", description = "查询库存记录列表")
    @PostMapping("/pageList")
    public Result<IPage<WmsStockRecord>> pageList(@Parameter(description = "查询参数") @RequestBody WmsStockRecordParam wmsStockRecordParam) {
        Page<WmsStockRecord> page = new Page<>(wmsStockRecordParam.getPage(), wmsStockRecordParam.getPageSize());
        WmsStockRecord wmsStockRecord = new WmsStockRecord();
        BeanUtils.copyProperties(wmsStockRecordParam, wmsStockRecord);
        QueryWrapper<WmsStockRecord> wrapper = QueryWrapperUtil.build(wmsStockRecord);
        IPage<WmsStockRecord> list = wmsStockRecordService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取库存记录详细信息", description = "获取库存记录详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "库存记录ID") @PathVariable("id") Long id) {
        return Result.success(wmsStockRecordService.getById(id));
    }
    @Operation(summary = "新增库存记录", description = "新增库存记录")
    @PostMapping
    public Result<?> save(@Parameter(description = "库存记录") @RequestBody WmsStockRecord wmsStockRecord) {
        wmsStockRecordService.saveRecord(wmsStockRecord);
        return Result.success();
    }

    @Operation(summary = "修改库存记录", description = "修改库存记录")
    @PutMapping
    public Result<?> update(@Parameter(description = "库存记录") @RequestBody WmsStockRecord wmsStockRecord) {
        wmsStockRecordService.updateRecord(wmsStockRecord);
        return Result.success();
    }

    @Operation(summary = "删除库存记录", description = "删除库存记录")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(wmsStockRecordService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<WmsStockRecord> util = new ExcelUtil<>(WmsStockRecord.class);
        util.importTemplateExcel(response, "库存记录");
    }

    @Operation(summary = "导出库存记录列表", description = "导出库存记录列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockRecordParam wmsStockRecordParam) {
        WmsStockRecord wmsStockRecord = new WmsStockRecord();
        BeanUtils.copyProperties(wmsStockRecordParam, wmsStockRecord);
        QueryWrapper<WmsStockRecord> wrapper = QueryWrapperUtil.build(wmsStockRecord);
        List<WmsStockRecord> list = wmsStockRecordService.list(wrapper);
        ExcelUtil<WmsStockRecord> util = new ExcelUtil<>(WmsStockRecord.class);
        util.exportExcel(response, list, "库存记录数据");
    }

    @Operation(summary = "导入库存记录列表", description = "导入库存记录列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WmsStockRecord> util = new ExcelUtil<>(WmsStockRecord.class);
        List<WmsStockRecord> wmsStockRecordList = util.importExcel(file.getInputStream());
        String message = wmsStockRecordService.importData(wmsStockRecordList, updateSupport);
        return Result.success(message);
    }
}